package com.mrx.pvzcommunity.service;

import com.mrx.pvzcommunity.dao.CommentDao;
import com.mrx.pvzcommunity.model.Response;
import com.mrx.pvzcommunity.model.Result;
import com.mrx.pvzcommunity.model.thread.Comment;
import com.mrx.pvzcommunity.model.user.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CommentService {

    private final CommentDao commentDao;

    public Result<List<Comment>> findCommentByUser(User user) {
        if (user == null) {
            return Result.createResult(Response.Code.USER_NOT_LOGGED, Collections.emptyList());
        }
        List<Comment> res = commentDao.findCommentByUid(String.valueOf(user.getUid()));
        if (res.isEmpty()) {
            return Result.createResult(Response.Code.NOT_FOUND, Collections.emptyList());
        }
        return Result.createResult(Response.Code.SUCCESS, res);
    }

    public Result<List<Comment>> findCommentByTid(String tid) {
        List<Comment> res = commentDao.findCommentByTid(tid);
        if (res.isEmpty()) {
            return Result.createResult(Response.Code.NOT_FOUND, Collections.emptyList());
        }
        return Result.createResult(Response.Code.SUCCESS, res);
    }

    public Result<String> addCommentByTid(String tid, User user, String content) {
        if (user == null) {
            return Result.createResult(Response.Code.USER_NOT_LOGGED);
        }
        if (commentDao.addCommentByTid(tid, String.valueOf(user.getUid()), content)) {
            return Result.createResult(Response.Code.SUCCESS);
        }
        return Result.createResult(Response.Code.UNKNOWN_ERROR);
    }
}
